﻿#region Header

// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------

#endregion

#region Usings

using System;
using System.Data;
using System.Data.Objects;
using System.Data.Objects.DataClasses;
using System.IO;
using System.Security;
using System.Security.Cryptography;
using System.Text;
using System.Reflection;

#endregion

namespace ScrumTable.Common.Data.Objects
{
    /// <summary>
    /// Represents an utility class for entity ObjectContext handling.
    /// </summary>
    public static class ObjectContextUtil
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------
        
        #endregion

        #region Constructors / Destructor

        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        #endregion

        #region Methods

        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Returns true if the given entity is attached to the current context.
        /// </summary>
        /// <param name="context">Specifies the context which should check for the attached state.</param>
        /// <param name="entity">Specifies the entity to check.</param>
        public static bool IsAttachedTo(this ObjectContext context, IEntityWithKey entity)
        {
            PreCondition.AssertNotNull(context, "context");
            PreCondition.AssertNotNull(entity, "entity");
            
            ObjectStateEntry entry; 
            if(context.ObjectStateManager.TryGetObjectStateEntry(entity, out entry)) { 
                return (entry.State != EntityState.Detached); 
            } 
            return false; 
        }

        /// <summary>
        /// Returns true if the given entity could be detached from the current context.
        /// </summary>
        /// <param name="context">Specifies the context which should be deatched.</param>
        /// <param name="entity">Specifies the entity to detach.</param>
        public static bool TryDetach(this ObjectContext context, IEntityWithKey entity)
        {
            PreCondition.AssertNotNull(context, "context");
            PreCondition.AssertNotNull(entity, "entity");

            if (context.IsAttachedTo(entity))
            {
                context.Detach(entity);
                return true;
            }
            return false;
        }

        #endregion

        #region Events

        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        #endregion
    }
}